package com.qc31.monitorlib.ui.carreport

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import autodispose2.autoDispose
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.loadsie.EmptyCallback
import com.qc31.baselib.rx.queryThrottle
import com.qc31.baselib.utils.formatTo2F
import com.qc31.baselib.utils.startActivityTo
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.FeeInfo
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentReportFeeBinding
import com.qc31.monitorlib.databinding.IncludeDayChooseBinding
import com.qc31.monitorlib.databinding.IncludeFeeInfoBinding
import com.qc31.monitorlib.ui.carmonitor.CarMonitorViewModel

/** @Author Jasper @Time 2020/10/21 @Desc 费用 */
class CarReportFeeFragment :
    LazyFragment<FragmentReportFeeBinding>(FragmentReportFeeBinding::inflate) {

    private lateinit var loadService: LoadService<Any>
    private lateinit var mDayChooseVB: IncludeDayChooseBinding
    private lateinit var mFeeInfoVB: IncludeFeeInfoBinding

    private val mActivityVM by activityViewModels<CarMonitorViewModel>()

    private val mViewModel: ReportItemViewModel by viewModels()

    override fun initView(view: View) {
        mDayChooseVB = IncludeDayChooseBinding.bind(binding.root)
        mFeeInfoVB = IncludeFeeInfoBinding.bind(binding.root)
        mViewModel.dataType = 4
        loadService = LoadSir.getDefault().register(binding.flFeeContent)
    }

    private var fee: FeeInfo? = null
    override fun initData() {
        mActivityVM.carId.observe(this, Observer {
            if (it.isNullOrEmpty()) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                mViewModel.carId = it
                if (!mViewModel.isHidden) mViewModel.handleTime()
            }
        })


        if (mViewModel.carId.isEmpty()) showToast(R.string.hint_toast_choose_car)
        loadService.showCallback(EmptyCallback::class.java)
        handleViewModel()
    }


    private fun handleViewModel() {
        val unitRmb = getString(R.string.unit_rmb)
        val unitRise = getString(R.string.unit_rise)
        mViewModel.feeDetailSub.hide().autoDispose(scopeProvider).subscribe {
            if (it.fees.isNotEmpty()) {
                loadService.showSuccess()
                fee = it.fees[0].apply {
                    mFeeInfoVB.lrTvFeeAddOil.setRightText("${formatTo2F(addOilFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeInsure.setRightText("${formatTo2F(insureFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeTraffic.setRightText("${formatTo2F(trafficFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeRepair.setRightText("${formatTo2F(repairFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeYear.setRightText("${formatTo2F(yearAdtFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeOther.setRightText("${formatTo2F(otherFee)} $unitRmb")
                    mFeeInfoVB.lrTvAddOil.setRightText("${formatTo2F(addOilCapacity)} $unitRise")
                    mFeeInfoVB.lrTvFeeTotal.setRightText("${formatTo2F(totalFee)} $unitRmb")
                }

            } else {
                loadService.showCallback(EmptyCallback::class.java)
            }
        }
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        mViewModel.feeInfoSub.hide().autoDispose(scopeProvider).subscribe {
            startActivityTo<FeeDetailActivity>(requireContext()) {
                putExtra(Keys.INTENT_DATA, it)
                putExtra(Keys.INTENT_OTHER, mViewModel.feeType)
            }
        }
        mViewModel.freeEmptySub.hide().autoDispose(scopeProvider).subscribe {
            if (it) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                loadService.showSuccess()
            }
        }
    }

    override fun initEvent() {
        mDayChooseVB.rdbReportYesterday.setOnClickListener {
            mViewModel.timeType = 1
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportToday.setOnClickListener {
            mViewModel.timeType = 2
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportThree.setOnClickListener {
            mViewModel.timeType = 3
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportWeek.setOnClickListener {
            mViewModel.timeType = 4
            mViewModel.handleTime()
        }

        mFeeInfoVB.lrTvFeeAddOil.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.addOilFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeAddOil")
            }
        }
        mFeeInfoVB.lrTvFeeInsure.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.insureFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeInsure")
            }

        }
        mFeeInfoVB.lrTvFeeTraffic.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.trafficFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeTraffic")
            }
        }
        mFeeInfoVB.lrTvFeeRepair.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.repairFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeRepair")
            }
        }
        mFeeInfoVB.lrTvFeeYear.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.yearAdtFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeYearVrf")
            }
        }
        mFeeInfoVB.lrTvFeeOther.queryThrottle().autoDispose(scopeProvider).subscribe {
            fee?.let {
                if (it.otherFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@subscribe
                }
                mViewModel.getFeeInfo("FeeOther")
            }
        }
    }

    //是否隐藏状态变化监听
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            mViewModel.isHidden = true
        } else {
            mViewModel.isHidden = false
            if (mViewModel.carId.isNotEmpty()) mViewModel.handleTime()

        }
    }
}